package apresentacao.caminhoneiro;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.caminhoneiro.R;

import controle.caminhoneiro.Controller_Conta;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Nova_Conta2 extends Activity implements OnClickListener,
		OnFocusChangeListener {

	Button btContinuar;
	EditText etEmail, etTelefone;
	String confirmar_Senha, senha, cpf, nome;
	Spinner spVeiculo, spCarroceria;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		nome			= getIntent().getExtras().getString("nome");
		cpf 			= getIntent().getExtras().getString("CPF");
		senha 			= getIntent().getExtras().getString("senha");
		confirmar_Senha = getIntent().getExtras().getString("confirmar_Senha");
		setContentView(R.layout.nova_conta2);
		inicializar();
	}

	public void inicializar() {
		ArrayAdapter<CharSequence> array_Veiculos = ArrayAdapter.createFromResource(this, R.array.Veiculos, android.R.layout.simple_spinner_item);
		array_Veiculos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		ArrayAdapter<CharSequence> array_Carroceria = ArrayAdapter.createFromResource(this, R.array.Carroceria, android.R.layout.simple_spinner_item);
		array_Carroceria.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spVeiculo = (Spinner) findViewById(R.id.Nova_Conta2_SpVeiculo);
		spVeiculo.setAdapter(array_Veiculos);
		spCarroceria = (Spinner) findViewById(R.id.Nova_Conta2_SpCarroceria);
		spCarroceria.setAdapter(array_Carroceria);
		btContinuar = (Button) findViewById(R.id.Nova_Conta2_BtCriar_Conta);
		etEmail 	= (EditText) findViewById(R.id.Nova_Conta2_EtEmail);
		etTelefone 	= (EditText) findViewById(R.id.Nova_Conta2_EtTelefone);
		etEmail.	setOnFocusChangeListener(this);
		btContinuar.setOnClickListener(this);
		etTelefone.	setOnFocusChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String telefone = etTelefone.getText().toString();
		String email = etEmail.getText().toString();
		Controller_Conta controle = new Controller_Conta();
		HashMap<String, String> hash = controle.verificar_Nova_Conta_Tela2(email, telefone, this);
		if(hash.size() == 0){
			
			// ADICIONAR CAMINHONEIRO
			
		}else{
			ArrayList<String> list = new ArrayList<String>();
			String email_Hash 	= hash.get("email");
			if(email_Hash != null){
				list.add(email_Hash);
			}
			String telefone_Hash 	= hash.get("telefone");
			if(telefone_Hash != null){
				list.add(telefone_Hash);
			}
			String erros[] = new String[list.size()];
			for(int i = 0; i <erros.length; i++){
				erros[i] = list.get(i);
			}
			ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, erros);
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setAdapter(array, null);
			alert.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			alert.setTitle("Erro");
			AlertDialog a = alert.create();
			a.show();
		}
	}

	@Override
	public void onFocusChange(View event, boolean hasFocus) {
		// TODO Auto-generated method stub
		EditText edit = (EditText) event;
		switch (event.getId()) {
			case R.id.Nova_Conta2_EtEmail: {
				if (hasFocus && etEmail.getText().toString().equals(this.getString(R.string.email))) {
					edit.setText("");
				} else if (!hasFocus && etEmail.getText().toString().equals("")) {
					edit.setText(this.getString(R.string.email));
				}
			}
			case R.id.Nova_Conta2_EtTelefone: {
				if (hasFocus && etTelefone.getText().toString().equals(this.getString(R.string.telefone))) {
					edit.setText("");
				} else if (!hasFocus && etTelefone.getText().toString().equals("")) {
					edit.setText(this.getString(R.string.telefone));
				}
			}
		}
	}

}
